package org.ihelpuoo.orm.handler.xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.io.FilePatternFilter;
import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.orm.IIIManual;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.Filter;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.LoopScope;
import org.ihelpuoo.util.Pager;
import org.ihelpuoo.xml.XMLWriter;
import org.w3c.dom.Document;

public class XmlFileProcessor extends ObjectProcessor
{

	public XmlFileProcessor(IIIManual manual)
	{
		super(manual);
	}

	@Deprecated
	@Override
	public boolean delete(ObjectRule r, Object src, Object obj, FieldFilter ignore) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support deleteBean()!");
	}

	@Deprecated
	@Override
	public int delete(ObjectRule r, Object src, List list, int off, int len) throws IIIException
	{
		throw new IIIException(this.getClass().getName() + " do not support clearBeans()!");
	}

	@Override
	public Object fetch(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked, String prefix) throws IIIException
	{
		try
		{
			Document doc = (Document)manual.dom().parse((File)src);
			return manual.getDomProcessor().fetch(r,doc.getDocumentElement(),obj,actived,locked, null);
		}
		catch (Exception e)
		{
			throw IIIException.wrap(e);
		}
	}

	@Override
	public Object insert(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked) throws IIIException
	{
		try
		{
			Document doc = (Document)manual.dom().parse((File)src);
			obj = manual.getDomProcessor().insert(r,doc.getDocumentElement(),obj,actived,locked);
			XMLWriter w = XMLWriter.wrap(new BufferedWriter(new FileWriter((File)src)));
			w.write(doc);
			w.close();
			return obj;
		}
		catch (Exception e)
		{
			throw IIIException.wrap(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List query(ObjectRule r, Object src, Class beanType, Filter filter,
			FieldFilter actived, FieldFilter locked, Pager pager, Object refer)
			throws IIIException
	{
		if (!((File)src).isDirectory())
		{
			throw new IIIException("\"" + src + "\" should be a directory!");
		}
		try
		{
			FilePatternFilter ff = FilePatternFilter.getInstance(filter.getString());
			File[] files = ((File)src).listFiles((FilenameFilter)ff);
			List list = new LinkedList();
			LoopScope ls = LoopScope.evaluate(pager,files.length);
			for (int i = ls.start; i < ls.max; i++)
			{
				Object b = beanType.newInstance();
				b = fetch(r,files[i],b,actived,locked, null);
				list.add(b);
			}
			return list;
		}
		catch (Exception e)
		{
			throw IIIException.wrap(e);
		}
	}

	@Override
	public Object update(ObjectRule r, Object src, Object obj, FieldFilter actived,
			FieldFilter locked) throws IIIException
	{
		try
		{
			Document doc = (Document)manual.dom().parse((File)src);
			obj = manual.getDomProcessor().update(r,doc.getDocumentElement(),obj,actived,locked);
			XMLWriter w = XMLWriter.wrap(new BufferedWriter(new FileWriter((File)src)));
			w.write(doc);
			w.close();
			return obj;
		}
		catch (Exception e)
		{
			throw IIIException.wrap(e);
		}
	}

}
